package common

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

type Pagination struct {
	Total      int `json:"total"`       //总记录数
	TotalPages int `json:"total_pages"` //总页码
	PerPage    int `json:"perpage"`     //每页记录数
	Page       int `json:"page"`        //当前页码
	PrePage    int `json:"pre_page"`    //上一页
	NextPage   int `json:"next_page"`   //下一页
}

type BaseFilter struct {
	Page     int `json:"page"`      // 页数
	PageSize int `json:"page_size"` // 分页数
}

type Json struct {
	Code    int         `json:"code"`
	Message string      `json:"message"`
	Data    interface{} `json:"data"`
}

type DataWithPagination struct {
	Pagination *Pagination `json:"pagination"`
	List       interface{} `json:"list"`
}

func Response(g *gin.Context, httpCode int, code int, message string, data interface{}) {
	g.JSON(httpCode, Json{
		code,
		message,
		data,
	})
}

func Success(g *gin.Context, data interface{}) {
	Response(g, http.StatusOK, 0, "success", data)
}

func InvalidRequest(g *gin.Context, err error) {
	Response(g, http.StatusBadRequest, http.StatusUnprocessableEntity, err.Error(), "")
}

func BadRequest(g *gin.Context, message string) {
	Response(g, http.StatusBadRequest, http.StatusBadRequest, message, "")
}

func InterServerError(g *gin.Context, message string) {
	Response(g, http.StatusInternalServerError, http.StatusInternalServerError, message, "")
}
